//
//  GetVideoViewController.m
//  HuateVideo
//
//  Created by Tiger on 12-5-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "GetVideoViewController.h"

@interface GetVideoViewController ()

@end

@implementation GetVideoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.view.backgroundColor = TOOLBAR_TINTCOLOR;
    
    [self performSelector:@selector(setStatusBar:) withObject:@"0" afterDelay:0.01];
    
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
    //imagePicker.videoMaximumDuration = 300.0f;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.mediaTypes = [NSArray arrayWithObject:@"public.movie"];
    imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    imagePicker.view.frame = CGRectMake(0, -20, 320, 480);
    [self.view addSubview:imagePicker.view];
    
    [imagePicker viewWillAppear:NO];
    [imagePicker viewDidAppear:NO];
}

- (void)setStatusBar:(NSString *)status
{
    if ([status isEqualToString:@"0"]) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }
    else {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self performSelector:@selector(setStatusBar:) withObject:@"1" afterDelay:0.01];
    self.view.frame = CGRectMake(0, 0, 320, 460);
}

//拍摄取消返回
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{
    [self dismissModalViewControllerAnimated:YES];
    [picker release];
    
    //[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}

//拍摄成功返回
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    //存到系统相册
    if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]))
    {
        UISaveVideoAtPathToSavedPhotosAlbum([url path],self,@selector(video: didFinishSavingWithError: contextInfo:),nil);
    }
    
    [self dismissModalViewControllerAnimated:YES];
    [picker release];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(!error)
    {
        self.title = @"Saved!";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Saved Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else
    {
        //self.title = @"Saved!";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        //CFShow([error localizedDescription]);
    }
    
}

@end
